
package View;

import Module.Project;
import Module.Role;
import Module.Session;
import Module.Team;
import Module.User;
import Shared.Server;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.*;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.*;
import java.io.File;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

/**
 *
 * @author CCD gruppe 2
 */
public class SessionPlan extends JPanel {

    private Server server;
    private int projectId;
    private JFileChooser fc;
    private String result="";
    private Project project;

    public SessionPlan(Server server, int projectId) {
        this.server = server;
        this.projectId = projectId;
    }

    /**
     * A method that creates a session plan .pdf
     * @throws IOException
     * @throws DocumentException
     */
    public void createPlan() throws IOException, DocumentException{
        fc = new JFileChooser();
        int returnVal = fc.showSaveDialog(SessionPlan.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            result = file.getAbsolutePath();
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(result + ".pdf"));
            document.open();
            Paragraph p = new Paragraph("Sesjonsplan", new Font(FontFamily.HELVETICA, 24));
            p.setAlignment(Element.ALIGN_CENTER);
            document.add(p);
            document.add(new Phrase("Introduksjon\n", new Font(FontFamily.HELVETICA, 16)));
            project = server.getProject(projectId);
            document.add(new Phrase(project.getDesc() + "\n\n"));
            document.add(new Phrase("Målsettning\n", new Font(FontFamily.HELVETICA, 16)));
            document.add(new Phrase(project.getGoals() + "\n\n"));
            ArrayList<Session> sessions = new ArrayList<Session>();
            sessions = server.getAllSessionsToProject(projectId);
            for (int i = 0; i < sessions.size(); i++) {
                document.add(new Phrase("Sesjon " + (i + 1), new Font(FontFamily.HELVETICA, 16)));
                document.add(sessionPlan(sessions.get(i)));
            }
            document.close();
        }
        else {
            System.out.println("Save command cancelled by user." + "/n");
        }
        
    }

    /**
     * A method that inserts session info into the session plan pdf table
     * @param session
     * @return
     */
    public PdfPTable sessionPlan(Session session){
        PdfPTable table = new PdfPTable(2);
        try {
            PdfPCell cell;
            table.addCell("Header");
            table.addCell("Beskrivelse");
            cell = new PdfPCell(new Phrase("Navn"));
            cell.setColspan(1);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(session.getName()));
            cell.setColspan(1);
            table.addCell(cell);
            table.addCell("Tidspunkt");
            table.addCell(session.getDate() + " - CET " + session.getStartTime() + "-" + session.getEndTime());

            ArrayList<Team> teams = server.getTeamInfoToSession(session.getId());
            ArrayList<User> teamUser = new ArrayList<User>();
            for(int i=0; i<teams.size();i++){
                table.addCell("Deltagere(" + teams.get(i).getName()+")");
                teamUser = server.getUsersToTeam(teams.get(i).getId());
                String users="";
                if(i==0){
                    //Generall support team fasilitator
                    for(int j=0; j<teamUser.size();j++){
                        ArrayList<String> roles = server.getRoleToUserSession(teamUser.get(j).getId(),session.getId());
                        users+=""+teamUser.get(j).getFullname()+" ," + teamUser.get(j).getWorkplace()+ "\n";
                       }

                } else {
                    for (int j = 0; j < teamUser.size(); j++) {
                        users += "" + teamUser.get(j).getFullname() + " ," + teamUser.get(j).getWorkplace()+ "\n";

                    }
                }
                table.addCell(users);
            }
            table.addCell("Forberedelse");
            table.addCell(session.getPreparation());
            for(int i=1;i<teams.size();i++){
                table.addCell("Målsetning for "  + teams.get(i).getName());
                table.addCell(teams.get(i).getGoal());
            }
            for(int i=1; i<teams.size();i++){
                table.addCell("Levering for "+ teams.get(i).getName());
                table.addCell(teams.get(i).getGoal());
            }
            table.addCell("Evaluering");
            table.addCell(session.getEvaluation());
            
            
        } catch (RemoteException ex) {
            Logger.getLogger(SessionPlan.class.getName()).log(Level.SEVERE, null, ex);
        }
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        return table;
    }
}
